package com.bjzc.AndroidSDK;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.http.util.EncodingUtils;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class ListeditActivity extends Activity {
    private ListView lv;
    private List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listedit);
        lv = (ListView) findViewById(R.id.listView1);
        initData();
        LvAdapter adapter = new LvAdapter(this, list);
        lv.setAdapter(adapter);
    }

    private void initData() {
        String ss = readZDData("wdlist");
        System.out.println(ss);
        if (ss.length() > 0) {
            StringTokenizer tokenizer = new StringTokenizer(ss, "\n\r");
            list.clear();
            while (tokenizer.hasMoreTokens()) {
                String row = tokenizer.nextToken();
                String[] arr = row.split("\t");
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("name", arr[0]);
                if (arr.length == 1) map.put("edit1", "");
                else map.put("edit1", arr[1]);
                if (arr.length == 1) map.put("edit2", "");
                else map.put("edit2", arr[2]);
                list.add(map);
            }
        } else
            for (int i = 0; i < 35; i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("name", "No." + i);
                map.put("edit1", "");
                map.put("edit2", "");
                list.add(map);
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.listedit, menu);
        MenuItem mn = menu.add(0, 1, Menu.NONE, "����");
        mn.setShowAsAction(1);
        menu.add(0, 2, Menu.NONE, "ˢ��").setShowAsAction(1);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                String nr = "";
                for (int i = 0; i < 35; i++) {
                    nr = nr + list.get(i).get("name") + "\t" + list.get(i).get("edit1") + "\t" + list.get(i).get("edit2") + "\r\n";
                }
                writeFileData("wdlist", nr);
                break;
            case 2:
                initData();
                ((LvAdapter) lv.getAdapter()).notifyDataSetChanged();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void writeFileData(String fileName, String message) {
        try {
            FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
            byte[] bytes = message.getBytes();
            fout.write(bytes);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String readZDData(String fileName) {
        String res = "";
        try {
            FileInputStream fin = openFileInput(fileName);
            int length = fin.available();
            byte[] buffer = new byte[length];
            fin.read(buffer);
            res = EncodingUtils.getString(buffer, "UTF-8");
            fin.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}
